package net.cz.wc.output;

import java.text.SimpleDateFormat;

import net.cz.wc.dao.BasicStatsDTO;
import net.cz.wc.main.Context;
import net.cz.wc.olddto.GCMetricsDTO;
import net.cz.wc.olddto.IntervalDTO;
import net.cz.wc.olddto.OldLogDTO;

public class ConsoleOutput {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd:kk:mm:ss");
	
	private static final String SEPARATOR = "  ";
	private static final String PIPE = "|";
	
	/**
	 * prints the default header for a metric list
	 */
	public static void printDefaultHeader() {
		String header = 
			"LOG" +
			getSpacesString(4) +
			"TIME" + 
			getSpacesString(17) +
			"METRIC" +
			getSpacesString(26) + 
			"VALUE" + 
			getSpacesString(10);
		
		Context.getContext().print(header);
	}	
	
	private static final String SPACE = " ";
	
	/**
	 * Returns a String of spaces with the size of the parameter size
	 * @param size size of the string
	 * @return String of spaces
	 */
	public static String getSpacesString(int size) {
		StringBuilder string = new StringBuilder("");
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				string.append(SPACE);
			}
		}
		
		return string.toString();
	}
	
	/**
	 * Fills a string with spaces to the right side until the string size 
	 * is endSize
	 * @param initialString string to be filled
	 * @param endSize size the string must have after the fill
	 * @return string with the spaces in the right side
	 */
	public static String fillStringWithRightSpaces (String initialString, int endSize) { 
		String filledString;
		if (initialString.length() >= endSize) {
			filledString = initialString;
		} else {
			String spaces = getSpacesString(endSize - initialString.length());
			
			filledString = initialString + spaces;
		}
		
		return filledString;
	}
	
	/**
	 * Fills a string with spaces to the left side until the string size 
	 * is endSize
	 * @param initialString string to be filled
	 * @param endSize size the string must have after the fill
	 * @return string with the spaces in the right side
	 */
	public static String fillStringWithLeftSpaces (String initialString, int endSize) { 
		String filledString;
		if (initialString.length() >= endSize) {
			filledString = initialString;
		} else {
			String spaces = getSpacesString(endSize - initialString.length());
			
			filledString = spaces + initialString;
		}
		
		return filledString;
	}
	
	/**
	 * Prints the QueryOutput with a default format
	 * @param out object to print
	 */
	public static void printDefault (QueryOutput out) {
		String metricNameOutput = null;
		
		if (out.getMetricName().length() > 30) {
			metricNameOutput = 
				out.getMetricName().substring(0, 10) + "..." +
				out.getMetricName().substring(out.getMetricName().length() - 17, out.getMetricName().length());
		} else {
			metricNameOutput = fillStringWithRightSpaces(out.getMetricName(), 30);
		}
		
		String metricValueOutput = fillStringWithRightSpaces(Long.toString(out.getMetricValue()), 15);
		
		String logIdOutput = fillStringWithRightSpaces(Long.toString(out.getLogId()), 5);
		
		String stringResult = 
				logIdOutput + 
				SEPARATOR +
				dateFormat.format(out.getMetricTime()) +
				SEPARATOR +
				metricNameOutput + 
				SEPARATOR + 
				metricValueOutput
				;
		
		Context.getContext().print(stringResult);
	}
	
	/**
	 * Prints the OldLogDTO with a default format
	 * @param out object to print
	 */
	public static void printDefault (OldLogDTO out) {
		System.out.println(
				out.getId() + " - " + out.getFileName() + " " + 
				out.getLoadTime() + " " + out.getFileType()
			);
	}
	
	/**
	 * Prints the log header
	 */
	public static void printDefaultLogHeader() {
		System.out.println("LOG ID    -    FILE NAME    -     LOAD TIME    -     FILE TYPE");
	}
	
	public static void printIntervalHeader() {
		String header = 
			"FROM" +
			getSpacesString(4) +
			"TO" + 
			getSpacesString(17) +
			"AVERAGE" +
			getSpacesString(26) + 
			"MAX" + 
			getSpacesString(10) +
			"MIN" +
			getSpacesString(10) +
			"COUNT" +
			getSpacesString(10);;
		
		Context.getContext().print(header);
	}
	
	public static void printInterval (IntervalDTO dto) {
		String line =
			dateFormat.format(dto.getStart()) +
			" - " +
			dateFormat.format(dto.getEnd()) +
			" - " +
			dto.getAverage() +
			" - " +
			dto.getMax() +
			" - " +
			dto.getMin() +
			" - " +
			dto.getCount();
		
		Context.getContext().print(line);
	}
	
	public static void print (BasicStatsDTO dto) {
		String line = 
			"Mean:                " + dto.getMean() + "\n" +
			"Max:                 " + dto.getMax() + "\n" +
			"Min:                 " + dto.getMin() + "\n" + 
			"Standar Deviation:   " + dto.getStandardDeviation() + "\n" +
			"Count:               " + dto.getCount();
		
		Context.getContext().print(line);
	}
	
	/**
	 * prints the header for a starndard GC metric list
	 */
	public static void printGCHeader() {
		String header = 
			"GC TYPE" +
			getSpacesString(7) +
			"TIMESTAMP" + 
			getSpacesString(7) +
			"USED HEAP" +
			getSpacesString(3) + 
			"HEAP AFTER GC" + 
			getSpacesString(2) +
			"TOTAL HEAP";
		
		Context.getContext().print(header);
	}
	
	/**
	 * prints the header for a standard GC metric list separated by pipes
	 */
	public static void printPipedGCHeader() {
		String header = 
			"GC TYPE" +
			PIPE +
			"TIMESTAMP" + 
			PIPE +
			"USED HEAP" +
			PIPE + 
			"HEAP AFTER GC" + 
			PIPE +
			"TOTAL HEAP";
		
		Context.getContext().print(header);
	}
	
	/**
	 * Prints the standard GC metrics of any GC collection event
	 * @param gcMetrics
	 */
	public static void printGCMetrics (GCMetricsDTO gcMetrics) {
		String line = 
			gcMetrics.getGcType() + 
			PIPE + 
			gcMetrics.getTimestamp() + 
			PIPE + gcMetrics.getUsedHeap() +
			PIPE + gcMetrics.getHeapAfterGC() + 
			PIPE + gcMetrics.getTotalHeap();
		
		Context.getContext().print(line);
	}
	
	/**
	 * 
	 * @param gcMetrics
	 */
	public static void printOnlyHeapMetrics (GCMetricsDTO gcMetrics) {
		String line = 
				gcMetrics.getUsedHeap() + PIPE + gcMetrics.getTotalHeap() + "\n" +
				gcMetrics.getHeapAfterGC() + PIPE + gcMetrics.getTotalHeap();
		
		Context.getContext().print(line);
	}
}
